package com.crayon.cardbiz;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

import com.crayon.cardbiz.dic.EntryDic;
import com.crayon.cardbiz.entry.FragmentChildPager20Set;
import com.crayon.cardbiz.fragment.FragmentListMenuLeft;
import com.crayon.cardbiz.fragment.FragmentListMenuRight;
import com.crayon.cardbiz.fragment.FragmentListMenuRoot.OnEntryMenuSelectListener;
import com.crayon.cardbiz.fragment.FragmentRoot;


/**
 * com.crayon.cardbiz.ActivityAppMain
 * @author kws
 *
 */
public class ActivityAppBody extends FragmentActivity implements OnClickListener
,OnEntryMenuSelectListener
,OnCheckedChangeListener
{
	private DrawerLayout mDrawerLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_main);

		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.context_main, new FragmentChildPager20Set());
		ft.commit();

		//		UI.bindOnClickEvent(this, R.id.toggleLeft, this);
		//		UI.bindOnClickEvent(this, R.id.toggleRight, this);

		ToggleButton toggleLeft= (ToggleButton)findViewById(R.id.toggleLeft);
		toggleLeft.setOnCheckedChangeListener(this);
		ToggleButton toggleRight= (ToggleButton)findViewById(R.id.toggleRight);
		toggleRight.setOnCheckedChangeListener(this);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerLayout.setDrawerListener(new DemoDrawerListener());
		mDrawerLayout.setDrawerShadow(new ColorDrawable(Color.YELLOW), GravityCompat.START);
	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch(v.getId()){
		case R.id.toggleLeft:

			break;
		case R.id.toggleRight:
			break;
		}

	}

	/**
	 * 1:1문의를 띄운다.
	 * (새로운 Fragment에서 띄워지는게 아니라, 현재 화면에서 팝업식으로 띄워지는 구조)
	 * 나중에 1:1문의를 선택했을때 호출되어지면 됨.
	 */
	private void sendFaqEmail(){
		Intent email = new Intent(Intent.ACTION_SEND);
		email.putExtra(Intent.EXTRA_EMAIL, new String[]{"T_album@theunus.com"});    
		email.putExtra(Intent.EXTRA_SUBJECT, "subject");
		email.putExtra(Intent.EXTRA_TEXT, "message");
		email.setType("message/rfc822");
		startActivity(Intent.createChooser(email, "Choose an Email client :"));		
	}

	@Override
	public void onSelectedEntry(EntryDic entry) {
		// TODO Auto-generated method stub
		if(entry==null) return;

		try {

			FragmentRoot fragment=(FragmentRoot) Class.forName(entry.cls.getName()).newInstance();
			if(fragment.getTopTitle()!=null){
				//					UI.bindText(this, R.id.textViewTopTilte, fragment.getTopTitle());
			}
			//					UI.bindText(this, R.id.textViewTopTilte, entry.name);
			//				UIBIND.bindText(this, R.id.textViewTopTilte, entry.name);
			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
			ft.replace(R.id.context_main, fragment);
			ft.commit();
		} catch (Exception e) {
			// TODO: handle exception
			
		}
		FragmentListMenuRight right = (FragmentListMenuRight)getSupportFragmentManager().findFragmentById(R.id.fragmentMenuRights);
		FragmentListMenuLeft left = (FragmentListMenuLeft)getSupportFragmentManager().findFragmentById(R.id.fragmentMenuLefts);
		mDrawerLayout.closeDrawer(right.getView());
		mDrawerLayout.closeDrawer(left.getView());
	}

	private class DemoDrawerListener implements DrawerLayout.DrawerListener {
		@Override
		public void onDrawerOpened(View drawerView) {

		}

		@Override
		public void onDrawerClosed(View drawerView) {

		}

		@Override
		public void onDrawerSlide(View drawerView, float slideOffset) {

		}

		@Override
		public void onDrawerStateChanged(int newState) {

		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch(buttonView.getId())
		{
		case R.id.toggleRight:
			FragmentListMenuRight right = (FragmentListMenuRight)getSupportFragmentManager().findFragmentById(R.id.fragmentMenuRights);
			if(isChecked)mDrawerLayout.openDrawer(right.getView());
			else mDrawerLayout.closeDrawer(right.getView());
			break;
		case R.id.toggleLeft:
			FragmentListMenuLeft left = (FragmentListMenuLeft)getSupportFragmentManager().findFragmentById(R.id.fragmentMenuLefts);
			if(isChecked)mDrawerLayout.openDrawer(left.getView());
			else mDrawerLayout.closeDrawer(left.getView());
			break;
		}
	}
}